package com.kaili.basic.controller.web.v1;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.OutboundApplication;
import com.kaili.basic.pojo.entity.OutboundApplicationExport;
import com.kaili.basic.pojo.entity.OutboundProduct;
import com.kaili.basic.service.OutboundApplicationService;
import com.kaili.basic.service.OutboundProductService;
import com.kaili.basic.service.PickManageService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 出库申请 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2019-03-07
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "出库申请", description = "出库申请相关API")
public class OutboundApplicationController {
    @Autowired
    OutboundApplicationService outboundApplicationService;
    @Autowired
    OutboundProductService outboundProductService;
    @Autowired
    private PickManageService pickManageService;

    @PostMapping(value = "/outboundApplications/addOutboundApplication")
    @ApiOperation(value = "新增出库申请",notes = "新增出库申请")
    public RestStatus addOutboundApplication(@RequestBody OutboundApplicationAddDto outboundApplicationAddDto){
        outboundApplicationService.addOutboundApplication(outboundApplicationAddDto);
        return CommonStatus.ADD_OK;
    }

    @PutMapping(value = "/outboundApplications/updateOutboundApplication")
    @ApiOperation(value = "修改出库申请",notes = "修改出库申请")
    public RestStatus updateOutboundApplication(@RequestBody OutboundApplicationUpdateDto outboundApplicationUpdateDto){
        outboundApplicationService.updateOutboundApplication(outboundApplicationUpdateDto);
        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/outboundApplications/deleteOutboundApplications")
    @ApiOperation(value = "根据主键id删除记录", notes = "根据主键id删除记录")
    public RestStatus deleteOutboundApplications(@RequestBody BusinessOperationDeleteDto deleteDto) {
        outboundApplicationService.deleteOutboundApplications(deleteDto);
        return CommonStatus.DELETE_OK;
    }

    @GetMapping(value = "/outboundApplications/queryOutboundApplication")
    @ApiOperation(value = "条件查询出库申请信息",notes = "条件查询出库申请信息")
    public Page<OutboundApplication> queryOutboundApplication(@ModelAttribute OutboundApplicationQueryDto outboundApplicationQueryDto) {
        return outboundApplicationService.queryOutboundApplication(new Page<OutboundApplication>(outboundApplicationQueryDto.getPage(),outboundApplicationQueryDto.getPageSize()),outboundApplicationQueryDto);
    }

    @GetMapping(value = "/outboundApplications/{applyId}/queryOutboundApplicationById")
    @ApiOperation(value = "根据出库申请主键id查询出库申请信息",notes = "根据出库申请主键id查询出库申请信息")
    public OutboundApplication queryOutboundApplicationById(@PathVariable("applyId") Long applyId) {
        return outboundApplicationService.queryOutboundApplicationById(applyId);
    }

    @GetMapping(value = "/outboundApplications/{id}/queryOutboundProductById")
    @ApiOperation(value = "根据出库产品主键id查询出库产品信息",notes = "根据出库产品主键id查询出库产品信息")
    public OutboundProduct queryWarehousingProductById(@PathVariable("id") Long id) {
        return outboundProductService.getOutboundProductById(id);
    }

    @PutMapping(value = "/outboundApplications/{applyId}/orderConfirmation")
    @ApiOperation(value = "订单确认",notes = "订单确认")
    public RestStatus orderConfirmation(@PathVariable("applyId") Long applyId) {
        OutboundApplication outboundApplication = new OutboundApplication();
        outboundApplication.setApplyId(applyId);
        outboundApplication.setOrderStatus(2);
        outboundApplication.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        outboundApplicationService.updateAffirmOrCancel(outboundApplication);
        return CommonStatus.ORDER_CONFIRMATION;
    }

    @PutMapping(value = "/outboundApplications/{applyId}/orderCancellation")
    @ApiOperation(value = "订单取消",notes = "订单取消")
    public RestStatus orderCancellation(@PathVariable("applyId") Long applyId) {
        OutboundApplication outboundApplication = new OutboundApplication();
        outboundApplication.setApplyId(applyId);
        outboundApplication.setOrderStatus(3);
        outboundApplication.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        outboundApplicationService.updateAffirmOrCancel(outboundApplication);
        return CommonStatus.ORDER_CANCEL;
    }

    @PostMapping(value = "/outboundApplications/queryWarehouseInventory")
    @ApiOperation(value = "点击出库并且确定时验证仓库可出库存",notes = "点击出库并且确定时验证仓库可出库存")
    public String queryWarehouseInventory(@RequestBody List<OutboundInventoryQueryDto> outboundInventoryQueryDtos) {
        for (OutboundInventoryQueryDto outboundInventoryQueryDto:outboundInventoryQueryDtos) {
            Long num=outboundApplicationService.queryWarehouseInventory(outboundInventoryQueryDto);
            if(num==null){
                num=0L;
            }
            if(outboundInventoryQueryDto.getQuantity().longValue()>num.longValue()){
                return "货品【"+outboundInventoryQueryDto.getProductName()+"】当前仓库可出库存："+num+"，不满足出库数："+outboundInventoryQueryDto.getQuantity()+"，请确保当前仓库有足够的货品";
            }
        }
        return null;
    }

    @GetMapping("/outboundApplications/getWellenOperationCount")
    @ApiOperation(value = "点击出库按钮时验证波次，二次分拣操作是否在进行", notes = "点击出库按钮时验证波次，二次分拣操作是否在进行")
    public RestStatus getWellenOperationCount(@RequestParam("wellenFormId") Integer wellenFormId){
        Integer count=pickManageService.getWellenOperationCount(wellenFormId);
        if(count>0){
            return CommonStatus.ORDER_WAVE_UNDERWAY;
        }
        return CommonStatus.ORDER_WAVE_SUCCESS;
    }

    @GetMapping(value = "/outboundApplications/getRequestCode")
    @ApiOperation(value = "获取申请单号",notes = "获取申请单号")
    public String getRequestCode() {
        Long id = outboundApplicationService.getMaxApplyId();
        id = id + 1L;
        return StringUtils.leftPad(id.toString(), 6, "0");
    }

    @GetMapping("/outboundApplications/export")
    @ApiOperation(value = "导出出库申请信息")
    public void export(HttpServletResponse response, @ModelAttribute OutboundApplicationQueryDto outboundApplicationQueryDto) throws RestException {
        List<OutboundApplicationExport> list = outboundApplicationService.queryOutboundApplicationExport(outboundApplicationQueryDto);
        ExcelUtil.exportExcel(list,"出库申请信息","sheet1", OutboundApplicationExport.class, UUID.randomUUID().toString()+".xls",response);
    }
}

